//
//  JTabBarController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/9.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "JTabBarController.h"
#import "HomeViewController.h"
#import "HealthRecordViewController.h"
#import "MyAskViewController.h"
#import "MyCenterViewController.h"
#import "JNavigationController.h"
#import "LZLoginViewController.h"
#import "ZMyAskViewController.h"
#import "JTAlertView.h"

#import "LZUserInfo.h"
@interface JTabBarController () <UITabBarControllerDelegate>{
    NSArray * _viewControllers;
    NSMutableArray <JNavigationController *>* _navs;
    NSArray * _titleName;
    NSArray * _image;
    NSArray * _selectimage;
}

@end

@implementation JTabBarController
+(void)load {
    
    UITabBarItem *item = [UITabBarItem appearance];
    NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
    attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialzeDatas];
    [self addViewControllerToTabbarControllerWithViewController];
    self.delegate = self;
    
}

- (void)initialzeDatas {
    _titleName = @[@"首页",@"健康档案",@"我的咨询",@"个人中心"];
    _image = @[@"table_index",@"table_archives",@"table_Consultation",@"ztable_center"];
    _selectimage =@[@"table_index_pre",@"table_archives_pre",@"table_Consultation_pre",@"table_center_pre"];
}

- (void) addViewControllerToTabbarControllerWithViewController {
    NSArray * VCs = [self sutupChildViewController];
    _navs = [@[] mutableCopy];
    [VCs enumerateObjectsUsingBlock:^(UIViewController * VC, NSUInteger idx, BOOL * _Nonnull stop) {
        [_navs addObject:[self addNavigationController:VC index:idx]];
    }];
    self.viewControllers = _navs;
    self.tabBar.tintColor = MYGREENColor;
    self.selectedIndex = 0;
}

// 添加子控制器
- (NSArray *)sutupChildViewController {
    HomeViewController * oneVC = [[HomeViewController alloc] init];
    oneVC.navigationItem.title = _titleName[0];
    HealthRecordViewController * twoVC = [[HealthRecordViewController alloc]init];
    twoVC.navigationItem.title = _titleName[1];
    ZMyAskViewController * threeVC = [[ZMyAskViewController alloc]init];
    threeVC.navigationItem.title = _titleName[2];
    MyCenterViewController * fourVC = [[MyCenterViewController alloc]init];
    fourVC.navigationItem.title = _titleName[3];
    _viewControllers = @[oneVC,twoVC,threeVC,fourVC];
    
    return _viewControllers;
}
//获得 导航控制器
- (JNavigationController *)addNavigationController:(UIViewController *)vc index:(NSInteger)idx{
    JNavigationController * nav = [[JNavigationController alloc] initWithRootViewController:vc];
    nav.tabBarItem.title = _titleName[idx];
    nav.tabBarItem.image = [UIImage imageNamed:_image[idx]];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:_selectimage[idx]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] ;
    return nav;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


//判断是否登录 并弹窗提醒
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (viewController == [tabBarController.viewControllers objectAtIndex:1]||viewController == [tabBarController.viewControllers objectAtIndex:2] ){
//        判断是否有uid 或者uid为空
        LZUserInfo * user = [LZUserInfo getUserInfo];
        if (!user.uid || [user.uid isEqualToString:@""]) {
        JTAlertView * alert = [[JTAlertView alloc] init];
                [alert showPleaseLoginFromviewController:self loginHandel:^{
                    LZLoginViewController * logVC = [[LZLoginViewController alloc] init];
                    JNavigationController * navVc = [[JNavigationController alloc] initWithRootViewController:logVC];
                    [self presentViewController:navVc animated:YES completion:nil];
                }];
                return NO;
    }
}
    return YES;
    
}




@end
